<?php

/**
 * Controller_maince
 * 维护单位设置
 * 公司名称 简称 人员 联系电话 用户名 密码
 */

class Controller_maince extends MyApp {


	/** 首页 **/
	public function actionIndex()
	{

	}
	//**------------------------------------- 维护单位设置  --------------------------------**//
	/** 维护单位设置列表 **/
	public function actionmaincelist ()
	{	
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_maince`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$maince_lists = $this->db->fetchAll("SELECT * FROM `tbl_maince` ORDER BY maince_id DESC LIMIT {$start}, ".PAGE_LIMIT."");


		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		// 导出excel
		if($this->get->output == "excel"){

			include_once("lib/phpexcel/PHPExcel.php");
			include_once("lib/phpexcel/PHPExcel/Writer/Excel2007.php");

			// 输出excel
			$objPHPExcel = new PHPExcel();

			// 输出表头
			$a = 0;
			foreach ($record_lists as $key => $value){
				$objPHPExcel->getActiveSheet()->setCellValue($this->letter_ini[$a] . 1, $value['title']);
				$a++;
			}

			// 输出数据 - 从第2行开始
			$i =2;
			foreach ($staffnew_lists as $key => $value){
				$a = 0;
				foreach ($record_lists as $k => $v){
					$objPHPExcel->getActiveSheet()->setCellValue($this->letter_ini[$a] . $i, (string)$value[$v['name']]);
					$a++;
				}
				$i++;
			}

			$objPHPExcel->createSheet();
			$fname = date("YmdHis").".xls";

			//保存excel—2007格式
			//$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
			//$objWriter->save("public/excel/xxx.xlsx");

			//直接输出到浏览器
			$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
			header("Pragma: public");
			header("Expires: 0");
			header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
			header("Content-Type:application/force-download");
			header("Content-Type:application/vnd.ms-execl");
			header("Content-Type:application/octet-stream");
			header("Content-Type:application/download");;
			header("Content-Disposition:attachment;filename={$fname}");
			header("Content-Transfer-Encoding:binary");
			$objWriter->save('php://output');

			exit;
		}


		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("maince_lists", $maince_lists);
		$this->tpl->display('maince/maince.list.tpl.php');

	}
	/** 维护单位添加 **/
	public function actionmainceAdd()
	{
		$maince_lists = $this->db->fetchAll("SELECT * FROM `tbl_maince`");

		if ($_POST){

			$maince_com    	    = $this->post->maince_com;
			$maince_rename  	= $this->post->maince_rename;
			$maince_name    	= $this->post->maince_name;
			$maince_phone    	= $this->post->maince_phone;
			$maince_user  	    = $this->post->maince_user;
			$maince_pwd   	    = $this->post->maince_pwd;	
			
            if (empty($maince_com)) {
           
            	Helper_Client::location("请填写报修公司名称", "index.php?a=shops.shopsadd");
            }
			if (empty($maince_rename)) {
           
            	Helper_Client::location("请填写报修公司简称", "index.php?a=shops.shopsadd");
            }
			if (empty($maince_name)) {
           
            	Helper_Client::location("请填写人员姓名", "index.php?a=shops.shopsadd");
            }
			if (empty($maince_phone)) {
           
            	Helper_Client::location("请填写联系电话", "index.php?a=shops.shopsadd");
            }
           	 if (!preg_match("/^[0-9]*$/", $maince_phone)) {
            	 	Helper_Client::location("联系电话要为真实电话", "index.php?a=maince.mainceadd");
            	 }

			// 写入数据库
			$info = array(

			"maince_com"            =>$maince_com,
			"maince_rename"         =>$maince_rename,
			"maince_name"            =>$maince_name,			
			"maince_phone"          =>$maince_phone,
			"maince_user"           =>$maince_user,
			"maince_pwd"            =>$maince_pwd,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_maince", $info);
            $mid = $this->db->insert_id();
            $infos = array(
                "user_name"           =>$maince_user,
                "user_password"            =>md5($maince_pwd),
                "user_type"             =>"maince",
                "type_id"             =>$mid,
                "status"                => 1,
                "create_time"           => date("Y-m-d H:i:s"),
                "update_time"           => "0000-00-00 00:00:00",

            );
            $this->db->do_insert("tbl_user", $infos);

			// 跳转
			Helper_Client::location("添加成功", "index.php?a=maince.maincelist");
		}

		$this->tpl->assign("maince_lists", $maince_lists);
		$this->tpl->display('maince/maince.add.tpl.php');
	}
	/** 维护单位设置修改 **/
	public function actionmainceUpdate()
	{

		$maince_id   = (int)$this->get->maince_id;
		$maince_data = $this->db->fetchROW("SELECT * FROM `tbl_maince` WHERE `maince_id`='{$maince_id}'");

		if (empty($maince_data)){
			Helper_Client::alert("维护部门人员不存在！");
		}

		if ($_POST){

			$maince_com    	    = $this->post->maince_com;
			$maince_rename  	= $this->post->maince_rename;
			$maince_name    	= $this->post->maince_name;
			$maince_phone    	= $this->post->maince_phone;
			$maince_user  	    = $this->post->maince_user;
			$maince_pwd   	    = $this->post->maince_pwd;	
            
           	 if (!preg_match("/^[0-9]*$/", $maince_phone)) {
            	 	Helper_Client::location("联系电话要为真实电话", "index.php?a=maince.maincelist");
            	 }
			// 写入数据库
			$info = array(

			"maince_com"            =>$maince_com,
			"maince_rename"         =>$maince_rename,
			"maince_name"            =>$maince_name,			
			"maince_phone"          =>$maince_phone,
			"maince_user"           =>$maince_user,
			"maince_pwd"            =>$maince_pwd,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",
			
			);
			$this->db->do_update("tbl_maince", $info, "maince_id='{$maince_id}'");
            $info = array(

                "user_name"           =>$maince_user,
                "user_password"            =>md5($maince_pwd),
                "status"                => 1,
                "update_time"            => date("Y-m-d H:i:s"),


            );
            $this->db->do_update("tbl_user", $info, "user_type = 'maince' and type_id='{$maince_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=maince.maincelist&maince_id={$maince_id}");
		}

		$this->tpl->assign("maince_data", $maince_data);
		$this->tpl->display('maince/maince.update.tpl.php');
	}
	/** 维护单位设置删除 **/
	public function actionmainceDel()
	{
		$maince_id = $this->get->maince_id;
		$this->db->query("DELETE  FROM `tbl_maince` WHERE maince_id='{$maince_id}'");
        $this->db->query("DELETE  FROM `tbl_user` WHERE user_type = 'maince' and type_id='{$maince_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=maince.maincelist");
	}

}

?>